package com.cevent.zhanniu.server.service;/**
 * Created by Cevent on 2020/11/21.
 */

import com.cevent.zhanniu.server.domain.ZncmsAdmin;
import com.cevent.zhanniu.server.domain.ZncmsAdminExample;
import com.cevent.zhanniu.server.dto.AdminDto;
import com.cevent.zhanniu.server.dto.PageDto;
import com.cevent.zhanniu.server.mapper.ZncmsAdminMapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.checkerframework.checker.units.qual.A;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @author cevent
 * @description
 * @date 2020/11/21 14:45
 */
@Service
public class ZmcmsAdminService {
    @Resource
    private ZncmsAdminMapper zncmsAdminMapper;

    public List<ZncmsAdmin> getAdmins(){
        ZncmsAdminExample zncmsAdminExample=new ZncmsAdminExample();
        zncmsAdminExample.createCriteria().andIdEqualTo(1);
        zncmsAdminExample.setOrderByClause("id desc");
        return zncmsAdminMapper.selectByExample(zncmsAdminExample);
    }

    //1.使用dto：先创建dto获取的对象
    public List<AdminDto> getAdminDto(){

        //引入github-pagehelper:pageNum（第几页）pageSize（每页条数）
        PageHelper.startPage(1,1);

        ZncmsAdminExample adminExample=new ZncmsAdminExample();
        //1.criteria创建条件
        //adminExample.createCriteria().andIdEqualTo(1);
        //adminExample.setOrderByClause("id desc");
        //2.查询结果集
        List<ZncmsAdmin> adminList=zncmsAdminMapper.selectByExample(adminExample);
        //3.根据dto转换entity
        List<AdminDto> adminDtoList=new ArrayList<AdminDto>();
        for(int i=0;i<adminList.size();i++){
            //BeanUtils是spring提供的工具类，用于实体类的复制
            ZncmsAdmin zncmsAdmin=adminList.get(i);
            AdminDto adminDto=new AdminDto();
            BeanUtils.copyProperties(zncmsAdmin,adminDto);
            adminDtoList.add(adminDto);
        }

        return adminDtoList;
    }

    //2.分页查询
    public void getAdminPageDTO(PageDto pageDto){
        PageHelper.startPage(pageDto.getCurrentPage(),pageDto.getInitPageNum());
        ZncmsAdminExample adminExample=new ZncmsAdminExample();
        List<ZncmsAdmin> adminList=zncmsAdminMapper.selectByExample(adminExample);
        //引用pageInfo<泛型>
        PageInfo<ZncmsAdmin> adminPageInfo=new PageInfo<>(adminList);
        pageDto.setSumPage(adminPageInfo.getTotal());

        List<AdminDto> adminDtoList=new ArrayList<>();
        for(int i=0;i<adminList.size();i++){
            ZncmsAdmin zncmsAdmin=adminList.get(i);
            AdminDto adminDto=new AdminDto();
            BeanUtils.copyProperties(zncmsAdmin,adminDto);
            adminDtoList.add(adminDto);
        }
        pageDto.setList(adminDtoList);
    }

    //3.新增管理员
    public void addAdmin(AdminDto adminDto){

        ZncmsAdmin zncmsAdmin=new ZncmsAdmin();
        try {
            Date now=new Date();
            String date=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(now);
            long time=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse(date).getTime();
            int timeInt=(int)(time/1000/60);
            adminDto.setCreateTime(timeInt);
            adminDto.setUpdateTime(timeInt);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        BeanUtils.copyProperties(adminDto,zncmsAdmin);
        zncmsAdminMapper.insert(zncmsAdmin);
    }
}
